package com.aosan.website.common.security.config

import feign.RequestInterceptor
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.oauth2.client.OAuth2ClientContext
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails

@RefreshScope
@Configuration
class OpenFeignConfiguration{

    /**
     * Feign 访问服务时携带OAuth2 Token
     *
     * @return RequestInterceptor
     */
    @Bean
    @ConditionalOnClass(OAuth2ClientContext::class)
    @ConditionalOnProperty("security.oauth2.client.client-id")
    @Suppress("SpringJavaInjectionPointsAutowiringInspection")
    fun oAuth2RequestInterceptor(
            oAuth2ClientContext: OAuth2ClientContext,
            resourceDetails: OAuth2ProtectedResourceDetails
    ):RequestInterceptor = OAuth2FeignRequestInterceptor(oAuth2ClientContext,resourceDetails)
}